import scrapy
from scrapy import Request

from goodDesignImage2018Spider.items import WaiMaiYuanItem


class MeishijSpider(scrapy.Spider):
    name = 'meishijSpider'
    allowed_domains = ['meishij.net']
    start_urls = 'https://www.meishij.net/fenlei/kuaishoucai/'

    def start_requests(self):
        yield Request(url=self.start_urls, callback=self.parse)

    def parse(self, response):

        all_dd = response.xpath("/html/body/div[2]/article/div[1]/dl/dd")

        i = 1
        for dd in all_dd:
            for item in dd.xpath("./strong"):
                waimaiyuan_item = WaiMaiYuanItem()
                waimaiyuan_item['item_name'] = item.xpath("./a/text()").get()
                waimaiyuan_item['item_parent_id'] = i
                waimaiyuan_item['item_remark'] = item.xpath("./a[1]/@href")[0].extract()
                waimaiyuan_item['item_biz_type'] = 0
                yield waimaiyuan_item
            i += 1
